<template>
  <div class="login-container">
    <!-- 头像区域 -->
    <div class="login-box">
      <div class="avatar_box">
        <img src="@/assets/img/1.png" alt="">
      </div>
      <el-form :model="loginFrom" ref="loginFromRef" :rules="loginFromRules" label-width="0" class="login_form" @submit.native.prevent>
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input v-model="loginFrom.username" type="text" prefix-icon="el-icon-user-solid"></el-input>
        </el-form-item>
        <!-- 登录密码 -->
        <el-form-item prop="password">
          <el-input v-model="loginFrom.password" show-password prefix-icon="iconfont icon-3702mima" @keyup.enter.native="login"></el-input>
        </el-form-item>
        <!-- 按钮区 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type=" info" @click="resetLoginFrom">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>

  export default {
    name: 'login',
    data () {
      return {
        loginFrom: {
          username: 'admin',
          password: '123456'
        },
        // 表单输入规则
        loginFromRules: {
          username: [
            { required: true, message: '请输入登录名', trigger: 'blur' },
            { min: 3, max: 10, message: '长度在 3 到 10个字符', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入登录密码', trigger: 'blur' },
            { min: 6, max: 15, message: '长度在 6 到 15个字符', trigger: 'blur' }
          ],
        }
      }
    },
    methods: {
      // 表单重置
      resetLoginFrom () {
        this.$refs.loginFromRef.resetFields();
      },
      // 表单验证
      login () {
        this.$refs.loginFromRef.validate(async valid => {
          if (!valid) return;
          const { data: res } = await this.$http.post('login', this.loginFrom)
          if (res.meta.status === 400) return this.$message({
            duration: 2000,
            message: '登录失败',
            type: 'error'
          });
          this.$message({
            duration: 600,
            message: '登录成功',
            type: 'success'
          });
          //登录成功后保存token，保存到客户端的sessionStorage中
          sessionStorage.setItem('token', res.data.token);
          //通过编程式导航到跳转到hone主页面
          this.$router.push('/home');
        })
      },
    }
  }
</script>

<style lang="less" scoped>
.login-container{
  position: relative;
  background:#2b4b6b ;
  height: 100%;
  .login-box{
    position: absolute;
   	/* 定位的百分比参照父容器 */
		left: 50%;
		top: 50%;
		/* 使用transform实现元素居中,百分比参照元素本身的宽高 */
		transform: translate(-50%,-50%);
    width: 450px;
    height: 300px;
    border-radius:3px;
    background: #fff;
  }
  .avatar_box{
   position: absolute;
    left: 50%;
    transform: translate(-50%,-50%);
    height: 130px;
    width: 130px;
    border: 1px soild #eee;
    border-radius:50%;
    box-shadow: 0 0 10px #ddd;
    padding: 10px;
    background: #fff;
    // overflow: hidden;
    img{
      width: 100%;
      height: 100%;
      border-radius:50%;
      background: #0fa9eb;
    }
  }
  .login_form{
   position: absolute;
   bottom: 0;
   width: 100%;
   padding: 20px;
   box-sizing:border-box;
  }
 .btns{
   display: flex;
   justify-content: flex-end;
 }
}
</style>